package uk.org.wookey.jpet;

public class DbColumn {
	private String tableName;
	private String columnName;
	private String aliasName;
	private String columnType;
	
	public DbColumn(String table, String column, String type) {
		init(table, column, null, type);
	}
	
	public DbColumn(String table, String column, String alias, String type) {
		init(table, column, alias, type);
	}

	private void init(String table, String column, String alias, String type) {
		tableName = table;
		columnName = column;
		aliasName = alias;
		columnType = type;
	}
	
	public String toString() {
		return getLongName();
	}
	
	public String toSql() {
		String res = tableName + "." + columnName;
		
		if (aliasName != null) {
			res += " AS " + aliasName;
		}
		
		return res;
	}
	
	public String getShortName() {
		return columnName;
	}
	
	public String getLongName() {
		return tableName + "." + columnName;
	}
	
	public String getDeclaration() {
		return columnName + " " + columnType;
	}
	
	@Override
	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		
		if (!(o instanceof DbColumn)) {
			return false;
		}
		
		if (toSql().equalsIgnoreCase(((DbColumn)o).toSql())) {
			return true;
		}
		
		return false;	
	}
}
